//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LmVolatilityModel.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Parameter.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::CLmVolatilityModel (boost::shared_ptr<QuantLib::LmVolatilityModel>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLmVolatilityModel = NULL;
#endif
	_ppLmVolatilityModel = &childNative;
    
}
Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::CLmVolatilityModel (QuantLib::LmVolatilityModel& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLmVolatilityModel = NULL;
#endif
	_ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (&childNative);
    
    _LmVolatilityModelOwner = owner;
    
}

Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::CLmVolatilityModel (CLmVolatilityModel^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLmVolatilityModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (copy->GetShared());
        
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::CLmVolatilityModel (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLmVolatilityModel = NULL;
#endif
	if (!t->IsSubclassOf(CLmVolatilityModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::CLmVolatilityModel (QuantLib::Handle<QuantLib::LmVolatilityModel>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLmVolatilityModel = &childNative;
	_ppLmVolatilityModel = &static_cast<boost::shared_ptr<QuantLib::LmVolatilityModel>>(childNative.currentLink());
    
    _LmVolatilityModelOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::CLmVolatilityModel (QuantLib::Handle<QuantLib::LmVolatilityModel> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLmVolatilityModel = &childNative;
	_ppLmVolatilityModel = &static_cast<boost::shared_ptr<QuantLib::LmVolatilityModel>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::CLmVolatilityModel (QuantLib::LmVolatilityModel childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLmVolatilityModel = NULL;
#endif
	_ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (new QuantLib::LmVolatilityModel (childNative));
    
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::~CLmVolatilityModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLmVolatilityModel != NULL)
    {
	    delete _ppLmVolatilityModel;
        _ppLmVolatilityModel = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::!CLmVolatilityModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLmVolatilityModel != NULL)
    {
	    delete _ppLmVolatilityModel;
    }
}
QuantLib::LmVolatilityModel& Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::GetReference ()
{
    if (_ppLmVolatilityModel == NULL) throw gcnew NativeNullException ();
	return **_ppLmVolatilityModel;
}
boost::shared_ptr<QuantLib::LmVolatilityModel>& Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::GetShared ()
{
    if (_ppLmVolatilityModel == NULL) throw gcnew NativeNullException ();
	return *_ppLmVolatilityModel;
}
QuantLib::LmVolatilityModel* Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::GetPointer ()
{
    if (_ppLmVolatilityModel == NULL) throw gcnew NativeNullException ();
	return &**_ppLmVolatilityModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LmVolatilityModel>& Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::GetHandle ()
{
	if (_phLmVolatilityModel == NULL)
	{
		_phLmVolatilityModel = new Handle<QuantLib::LmVolatilityModel> (*_ppLmVolatilityModel);
	}
	return *_phLmVolatilityModel;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::HasNative () 
{
	return (_ppLmVolatilityModel != NULL);
}

Double Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::IntegratedVariance (UInt64 i, UInt64 j, Double u, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j);
        QuantLib::Time _u = (QuantLib::Time)ValueHelper::Convert (u);
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x))
        {
            _Cx = safe_cast<CArray^> (x->Value);
            _Cx->Lock();
        }
        QuantLib::Array& _x = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x) ? static_cast<QuantLib::Array&> (_Cx->GetReference ()) : QuantLib::Null<QuantLib::Array>()); //6
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLmVolatilityModel)->integratedVariance ( _i,  _j,  _u,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Cephei::IVector<Cephei::QL::Models::IParameter^>^ Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::P_params::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	std::vector<QuantLib::Parameter> & _rv = (std::vector<QuantLib::Parameter> &)(*_ppLmVolatilityModel)->params ( );   
        CoVector<Cephei::QL::Models::IParameter^>^ _nrv = gcnew CoVector<Cephei::QL::Models::IParameter^>(gcnew CParameterVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Legacy::Libormarketmodels::ILmVolatilityModel^ Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::SetParams (Cephei::IVector<Cephei::QL::Models::IParameter^>^ arguments)
{
    CoVector<Cephei::QL::Models::IParameter^>^ _Carguments;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Carguments = safe_cast<CoVector<Cephei::QL::Models::IParameter^>^> (arguments);
        _Carguments->Lock();
        INativeVector<Cephei::QL::Models::IParameter^>^ _NCIarguments = _Carguments->getFeature (NativeFeature::Value);
        CParameterVector^ _NCarguments = safe_cast<CParameterVector^>(_NCIarguments);
        std::vector<QuantLib::Parameter>& _arguments = static_cast<std::vector<QuantLib::Parameter>&> (_NCarguments->GetReference ());
    	(*_ppLmVolatilityModel)->setParams ( _arguments );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Carguments != nullptr) _Carguments->Unlock();
    }
}
UInt64 Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLmVolatilityModel)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Legacy::Libormarketmodels::CLmVolatilityModel::Volatility (UInt64 i, Double t, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x))
        {
            _Cx = safe_cast<CArray^> (x->Value);
            _Cx->Lock();
        }
        QuantLib::Array& _x = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x) ? static_cast<QuantLib::Array&> (_Cx->GetReference ()) : QuantLib::Null<QuantLib::Array>()); //6
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppLmVolatilityModel)->volatility ( _i,  _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

